package com.informationsystem.model;

import com.informationsystem.model.BaseObject;

import org.hibernate.search.annotations.DocumentId;
import org.hibernate.search.annotations.Field;
import org.hibernate.search.annotations.Indexed;
import org.hibernate.search.annotations.IndexedEmbedded;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.PrimaryKeyJoinColumn;
import javax.persistence.Table;
import javax.persistence.Transient;

import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;

import javax.xml.bind.annotation.XmlRootElement;

import java.io.Serializable;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Entity
@Table(name="video")
@Indexed
@XmlRootElement
public class Video extends Information implements Serializable {
    private String youtubeUrl;
    private String description;
    
    @Column(name="youtube_url", nullable=false, length=300)
    @Field
    public String getYoutubeUrl() {
        return this.youtubeUrl;
    }
    
    public void setYoutubeUrl(String youtubeUrl) {
        this.youtubeUrl = youtubeUrl;
    }
    
    @Column(name="description", nullable=false, length=1000)
    @Field
    public String getDescription() {
        return this.description;
    }
    
    public void setDescription(String description) {
        this.description = description;
    }

    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        Video pojo = (Video) o;

        if (youtubeUrl != null ? !youtubeUrl.equals(pojo.youtubeUrl) : pojo.youtubeUrl != null) return false;
        if (description != null ? !description.equals(pojo.description) : pojo.description != null) return false;

        return true;
    }

    public int hashCode() {
        int result = 0;
        result = 31 * result + (youtubeUrl != null ? youtubeUrl.hashCode() : 0);
        result = 31 * result + (description != null ? description.hashCode() : 0);

        return result;
    }

    public String toString() {
        StringBuffer sb = new StringBuffer(getClass().getSimpleName());

        sb.append(" [");
        sb.append("id").append("='").append(getId()).append("', ");
        sb.append("youtubeUrl").append("='").append(getYoutubeUrl()).append("', ");
        sb.append("description").append("='").append(getDescription()).append("'");
        sb.append("]");
      
        return sb.toString();
    }
    
	@Transient
    public String getDisplayYoutubeUrl(){
		String iframe = "";
    	Pattern pattern = Pattern.compile("[\\?&]v=([^&#]*)");
    	Matcher matcher = pattern.matcher(youtubeUrl); 
    	if(matcher.find())
    		iframe = matcher.group(1);
    	return iframe;
    }
	
}
